package com.example.gzysmartcity.ui.fragment.traffic;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentParkBinding;
import com.example.gzysmartcity.repository.model.CityPark;
import com.example.gzysmartcity.ui.adapter.ParkAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.ParkViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ParkFragment extends BaseFragment<FragmentParkBinding> {
    @Override
    protected FragmentParkBinding viewBinding() {
        return FragmentParkBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ParkViewModel parkViewModel=new ViewModelProvider(this).get(ParkViewModel.class);
        b.rvPark.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL,false));
        parkViewModel.getParkData();

        parkViewModel.getParkliveData().observe(getViewLifecycleOwner(),state -> {
            switch (state){
                case SUCCESS:
                    List<CityPark.DataBean> parklist=parkViewModel.getParksList();
                    ParkAdapter adapter=new ParkAdapter(R.layout.rv_item_park);
                    adapter.setNewInstance(parklist);
                    b.rvPark.setAdapter(adapter);
                    CityPark.DataBean p=parklist.get(0);
                    b.tvParkname.setText("停车场名称: "+p.getCityParkName());
                    b.tvParkwei.setText("停车场位剩余: "+p.getSpaceNum());
                    b.tvParkmoney.setText("收费标准:"+p.getPriceRate()+"/h");
                    adapter.setOnItemClickListener((adapter1, view1, position) -> {
                        CityPark.DataBean p1 =parklist.get(position);
                        adapter.setP(position);
                        adapter.notifyDataSetChanged();
                        b.tvParkname.setText("停车场名称: "+ p1.getCityParkName());
                        b.tvParkwei.setText("停车场位剩余: "+ p1.getSpaceNum());
                        b.tvParkmoney.setText("收费标准:"+ p1.getPriceRate()+"/h");
                    });
                    break;
                case FAIL:
                    ToastUtils.showShort("请求失败");
                    break;
                case NOTHING:
                    break;
            }
        });
    }

    @Override
    protected String setTitle() {
        return "停车场详情";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }
}
